home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6748 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  102 lines

  1. Path: winternet.com!mschwarz
  2. From: mschwarz@winternet.com (Michael Schwarz)
  3. Newsgroups: comp.lang.c
  4. Subject: Watch out!  C "gotcha!"
  5. Date: 14 Feb 1996 20:43:29 GMT
  6. Organization: StarNet Communications, Inc
  7. Message-ID: <4fthhh$7th@blackice.winternet.com>
  8. NNTP-Posting-Host: subzero.winternet.com
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Many of you may already know this, but even after ten years of coding
  12. in C, this took my by surprise at first.  A co-worker and I were looking
  13. at some library C code where I work and we noticed that the "default"
  14. clause of one of the switches was misspelled as "defalt."
  15.  
  16. He said, "This must have been changed since it was last compiled."
  17.  
  18. "What the heck, we're in development.  Run make."
  19.  
  20. (We're on an RS-6000 here, BTW).  Make ran and silently compiled the
  21. module.  No warning.  No error.  We fired the editor back up and I
  22. changed it to "token" and compiled again.  No warning, no error.
  23.  
  24. I came to the (in retrospect foolish) conclusion that the compiler must
  25. take any != 'case' string as the default.  I asked him to try it under
  26. Microsoft Visual C/C++ 1.0 while I went and tried it with Borland 3.1
  27.  
  28. Borlaqnd compiled without warning or error.  While I was at it, I tried
  29. running the following simple program:
  30.  
  31. #include <stdio.h>
  32.  
  33. main()
  34. {
  35.    int x = 2;
  36.  
  37.    switch (x)
  38.    {
  39.       case 1:
  40.          printf("It's 1.\n");
  41.          break;
  42.  
  43.       defalt:
  44.          printf("It's not 1.\n");
  45.          break;
  46.    }
  47.  
  48.    return 0;
  49. }
  50.  
  51.  
  52. No output.  Now this was a little more serious.  Then it hit me.  How about
  53. this version:
  54.  
  55.    int x = 1;
  56.  
  57.    switch (x)
  58.    {
  59.       case 1:
  60.          printf("It's 1.\n");
  61.          goto defalt;
  62.          break;
  63.  
  64. Sure enough.  The output of the program now is:
  65.  
  66. It's 1.
  67. It's not 1.
  68.  
  69. My co-worker came back and said it compiled but there was an unreferenced
  70. label warning.  Well, of course there was!
  71.  
  72. My point is that several compilers we tried this on compiled this mistake
  73. with no error or warning.  If you, like so many of us, do not get enough
  74. time to completely coverage test your code, this mistake can really bite
  75. you on the behind.  Your "default" code will never be executed!
  76.  
  77. Does anyone know of:
  78.  
  79. 1)    A specific remedy for the "cc/xlc" compilers under AIX, perhaps
  80.     an option that turns on a warning similar to the one we got from
  81.     MSVC?
  82.  
  83. 2)    A general workaround for this, e.g., something that will always
  84.     catch this.
  85.  
  86. 3)    The ANSI party line on labels -- is there an ANSI enforcement option
  87.     that guarantees a warning on a construct like this?  (If not, does
  88.     anyone know how to suggest revisions to the X3-J11 standard?)
  89.  
  90. The thing that scares me the most about this is that either I've never before
  91. made this particular mistake or for the last ten years I've been leaving
  92. behind a number of ticking code bombs...
  93.  
  94. I invite your comments on this (and your knowledge -- I'm a good C programmer
  95. but I don't write compilers and I'm not fully conversant with the full
  96. ANSI spec on C).  Please feel free to e-mail me.
  97.  
  98. --
  99. Michael A. Schwarz                           Minneapolis, MN
  100. mschwarz@winternet.com                  n0zes@n0zes.ampr.org
  101.  
  102.